<template>
  <view>
    <ugo-search></ugo-search>
    <u-tabs
      :list="list"
      :is-scroll="false"
      :current="current"
      @change="change"
      :bar-width="150"
      active-color="red"
      :active-item-style="{color:'black'}"
    ></u-tabs>
    <!-- 3 tabs对应内容 -->
    <view v-if="current===0">
      <navigator
        v-for="item in goods"
        :key="item.goods_id"
        :url="'/pages/goods_detail/goods_detail?goods_id='+item.goods_id"
      >
        <view class="goods-item">
          <view class="goods-img">
            <u-image width="191rpx" height="191rpx" :src="item.goods_small_logo"></u-image>
          </view>
          <view class="goods-info">
            <view class="goods-name u-line-2">{{item.goods_name}}</view>
            <view class="goods-price">￥ {{item.goods_price}}</view>
          </view>
        </view>
      </navigator>
    </view>
    <view v-else-if="current===1">销量的内容</view>
    <view v-else-if="current===2">价格的内容</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {
          name: "综合"
        },
        {
          name: "销量"
        },
        {
          name: "价格"
        }
      ],
      current: 0,
      // 接口要求 参数
      params: {
        // 否	string	关键字
        query: "",
        // 否	string	分类id
        cid: "",
        // 否	number	页码  1
        pagenum: 1,
        // 否	number	页容量  20条
        pagesize: 20
      },
      goods: [],
      // 接口数据的总长度
      total: 0
    };
  },
  methods: {
    change(index) {
      this.current = index;
    },
    async getGoods() {
      const res = await this.$u.get("/goods/search", this.params);
      this.goods = [...this.goods, ...res.message.goods];
      this.total = res.message.total;
    }
  },
  onLoad(option) {
    this.params.cid = option.id;
    this.getGoods();
  },
  // 滚动条触底
  onReachBottom() {
    if (this.goods.length >= this.total) {
      uni.showToast({
        title: "没有更多数据了",
        icon: "none"
      });

      return;
    }
    this.params.pagenum++;
    this.getGoods();
  },
  async onPullDownRefresh() {
    //     重置
    // 1 重置页面中显示的数据 goods=[]
    // 2 重置 参数 页码  1
    // 3 重新发送请求 获取数据
    this.goods = [];
    this.params.pagenum = 1;
    await this.getGoods();
    uni.stopPullDownRefresh();
  }
};
</script>
<style lang="scss">
.goods-item {
  display: flex;
  margin-top: 10rpx;
  margin-left: 10rpx;
  border-bottom: 1rpx solid #ccc;
  .goods-info {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    margin-left: 35rpx;
    .goods-name {
      font-size: 28rpx;
    }
    .goods-price {
      color: $backColor;
    }
  }
}
</style>